dojo.provide("lotex.widget.Dialog");

dojo.require("dojo.widget.*");
dojo.require("dojo.widget.Dialog");

dojo.widget.defineWidget(
    "lotex.widget.Dialog",
    dojo.widget.Dialog,
    {
        // summary
        //  Pops up a modal dialog window, blocking access to the screen and also graying out the screen
        //  Dialog is extended from ContentPane so it supports all the same parameters (href, etc.)
        templateString: "",
        templateCssString: "",
        templateCssPath: dojo.uri.moduleUri('lotex.widget.templates') + "Dialog.css",
        title: '..TITLE..',
        templatePath: dojo.uri.moduleUri('lotex.widget.templates') + "Dialog.html",

        fillInTemplate: function(){

            this.drag = new dojo.dnd.HtmlDragMoveSource(this.domNode);
            if (this.constrainToContainer) {
                this.drag.constrainTo();
            }
            this.drag.setDragHandle(this.titleBar);
            // dojo.event.connect(this.domNode, "onclick", this, "killEvent");
        },

        placeModalDialog: function() {
            //if(this.isShowing()) return;
            // summary: position modal dialog in center of screen

            var scroll_offset = dojo.html.getScroll().offset;
            var viewport_size = dojo.html.getViewport();

            // find the size of the dialog (dialog needs to be showing to get the size)
            var mb;
            var cb;
            var bb;
            if(this.isShowing()){
                mb = dojo.html.getMarginBox(this.domNode);
                cb = dojo.html.getMarginBox(this.containerNode);
                bb = dojo.html.getMarginBox(this.titleBar);
            }else{
                dojo.html.setVisibility(this.domNode, false);
                dojo.html.show(this.domNode);
                mb = dojo.html.getMarginBox(this.domNode);
                cb = dojo.html.getMarginBox(this.containerNode);
                bb = dojo.html.getMarginBox(this.titleBar);
                dojo.html.hide(this.domNode);
                dojo.html.setVisibility(this.domNode, true);
            }

            with(this.domNode.style){
                width = cb.width + "px";
                height = cb.height + bb.height + "px";
            }

            var x = scroll_offset.x + (viewport_size.width - mb.width)/2;
            var y = scroll_offset.y + (viewport_size.height - mb.height)/2;
            with(this.domNode.style){
                left = x + "px";
                top = y + "px";
            }
        }
    }
);